package cn.edu.hunau.ex.service;

import cn.edu.hunau.ex.pojo.Employee;

import java.io.*;
import java.nio.charset.Charset;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * @author hb
 * @date 2025-08-15 09:58:37
 */
public class EmpMangerService {
    private Map<Integer,Employee> emps;

    public EmpMangerService( ) {
        emps=new HashMap<>();
        //加载数据
        readFileLoadData();
        System.out.println(emps.size());
    }

    //提供一个方法维护员工的编号
    //对id最大值进行自增
    public Integer getEmpId(){
      int max=emps.keySet().stream().max(Integer::compareTo).get();
      return max+1;
    }


    // 获得岗位信息
    public Set<String> jobs(){
        // Set<String>  result=new HashSet<>();
        // for (Employee emp : emps.values()) {
        //    result.add(emp.getJob());
        // }
        Set<String> result = emps.values().stream()
                .collect(Collectors.mapping(Employee::getJob, Collectors.toSet()));
        return result;
    }

    /**
     * 将一个Employee的实例写入到emps.txt文件中
     * @param emp
     */
    public void writerEmpoyeeInfo(Employee emp){
        try(FileOutputStream fos=new FileOutputStream("dir/emps.txt",true);
            PrintWriter pw=new PrintWriter(fos,true, Charset.forName("utf-8"));){
             //1001:孙悟空:2000.0:程序员:12345678901:2020-01-01
            pw.println(emp);
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }

    public void readFileLoadData(){
        try(FileInputStream fis=new FileInputStream("dir/emps.txt");
            InputStreamReader isr=new InputStreamReader(fis,"utf-8");
            BufferedReader br=new BufferedReader(isr);){
            String line = null;
            while((line=br.readLine())!=null){
                Employee emp=  parseEmployee(line);
                //\d{4,6}:[\u4e00-\u9fa5]{2,4}:\d+\.\d+:[^\u0000-\u007F\u4e00-\u9fa5]*[\u4e00-\u9fa5]+:[1-9]\d{10}:\d{4}-\d{2}-\d{2}:(null|\d{4})
                emps.put(emp.getId(),emp);
            }
        }catch(Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }

    /*
     * 将字符串解析为Employee的实例
     * 1001:孙悟空:2000.0:程序员:12345678901:2020-01-01:null
     * 1002:猪八戒:12000.0:程序员:12345678901:2020-12-01:1001
     */
    private Employee parseEmployee(String line) {
        //[1001,孙悟空,2000.0,程序员,12345678901,2020-01-01,null]
        String[] datas = line.split(":");
        Employee emp=new Employee();
        emp.setId(Integer.parseInt(datas[0]));
        emp.setName(datas[1]);
        emp.setSalary(Double.parseDouble(datas[2]));
        emp.setJob(datas[3]);
        emp.setTelphone(datas[4]);
        emp.setIntoDate(LocalDate.parse(datas[5]));
        emp.setManagerId("null".equals(datas[6])?null:Integer.parseInt(datas[6]));

        return emp;
    }

    /**
     * 获得管理者以及下属的信息
     * @return
     */
    public Map<Integer,List<Employee>> manager(){
        /*Map<Integer,List<Employee>> manager = new HashMap<>();
        Collection<Employee> empsList = emps.values();
        for(Employee e:empsList){
            Integer mid = e.getManagerId();
            if(mid!=null){
                List<Employee> list = manager.getOrDefault(mid, new ArrayList<>());
                list.add(e);
                manager.put(mid,list);
            }
        }*/

         Map<Integer,List<Employee>> manager = emps.values().stream()
                .filter(e -> e.getManagerId() != null)
                .collect(Collectors.groupingBy(Employee::getManagerId));

        return manager;
    }
}
